package com.fs.controller;

import com.fs.annotation.SysLog;
import com.fs.mapper.SysAdminRoleMenuMapper;
import com.fs.pojo.SysAdminRoles;
import com.fs.service.SysAdminRoleMenuService;
import com.fs.service.SysAdminRoleService;
import com.fs.vo.PageResult;
import com.fs.vo.SysResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(tags = "角色管理")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private SysAdminRoleService roleService;

    @Autowired
    private SysAdminRoleMenuService sysAdminRoleMenuService;

    @SysLog("获取角色列表")
    @ApiOperation(value = "获取角色列表")
    @RequiresPermissions({"admin:role:list"})
    @GetMapping("/list")
    public SysResult getRoleList(PageResult request){
        return roleService.getList(request);
    }

    @SysLog("保存角色")
    @ApiOperation(value = "保存角色")
    @PostMapping("/save")
    @RequiresPermissions("admin:role:save")
    public SysResult save(@RequestBody SysAdminRoles role){
//        ValidatorUtils.validateEntity(role);

//        role.setCreateUserId(getUserId());
        return roleService.saveRole(role);
    }

    /**
     * 删除角色
     */
    @SysLog("删除角色")
    @ApiOperation(value = "删除角色")
    @PostMapping("/delete")
    @RequiresPermissions("admin:role:delete")
    public SysResult delete(@RequestBody Long[] roleIds){
        return roleService.deleteBatch(roleIds);
    }

    /**
     * 角色信息
     */
    @SysLog("角色信息")
    @ApiOperation(value = "角色信息")
    @GetMapping("/info/{roleId}")
    @RequiresPermissions("admin:role:info")
    public SysResult info(@PathVariable("roleId") Long roleId){
        SysAdminRoles role = roleService.getById(roleId);

        //查询角色对应的菜单
        List<Integer> menuIdList = sysAdminRoleMenuService.queryMenuIdList(roleId);
        role.setMenuIdList(menuIdList);

        return  SysResult.success(role);
    }

    /**
     * 修改角色
     */
    @SysLog("修改角色")
    @ApiOperation(value = "修改角色")
    @PostMapping("/update")
    @RequiresPermissions("admin:role:update")
    public SysResult update(@RequestBody SysAdminRoles role){
//        ValidatorUtils.validateEntity(role);

//        role.setCreateUserId(getUserId());
        return roleService.update(role);
    }
}
